﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.ApTrust
{
    public partial class ucApTrust :UserControl
    {
        protected DbWrapper.DbDataTypes.Ap m_apinfo = null;
        public ucApTrust()
        {
            InitializeComponent();
        }

        public bool bReadOnly = false;
        public bool bSnmpAccountEnable = false;
        public bool bDisplaySnmpModifyButton = false;

        public void InitWith(DbWrapper.DbDataTypes.Ap ap)
        {
            m_apinfo = ap;
            LoadAp();
        }

        public virtual void LoadAp()
        {
            //楼层下拉框初始化
            try
            {
                List<BusinessWrapper.Types.GroupInfo> listGroup =
                    BusinessWrapper.User.Instance.GetGroup();
                foreach (BusinessWrapper.Types.GroupInfo group in listGroup)
                {
                    SnmpForm.APConfig.ApConfigItem item =
                        new APConfig.ApConfigItem(group.name, group.id);
                    comboBoxFloodID.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            if (bReadOnly)
            {
                textBoxMac.ReadOnly = true;
                ipAddressControlIp.ReadOnly = true;
                comboBoxFloodID.Enabled = false;
                textBoxRoomID.ReadOnly = true;
                textBoxSnmpUser.ReadOnly = true;

                //密码不初始化
                textBoxSnmpPasswd.Text = "";
                textBoxSnmpPasswd.ReadOnly = true;
            }

            textBoxSnmpUser.Enabled = bSnmpAccountEnable;
            textBoxSnmpPasswd.Enabled = bSnmpAccountEnable;

            //是否显示snmp账户修改按钮
            buttonModifySnmp.Visible = bDisplaySnmpModifyButton;

            Update();
        }
        private bool CheckInputValid()
        {
            //检查楼层号
            string strText = comboBoxFloodID.Text;
            if (strText.Length > 100)
            {
                MessageBox.Show("楼层号太长");
                return false;
            }

            //检测房间号
            strText = textBoxRoomID.Text;
            if (strText.Length > 100)
            {
                MessageBox.Show("房间号太长");
                return false;
            }
            //snmp密码长度至少为8位
            strText = textBoxSnmpPasswd.Text;
            if (strText.Length < 8)
            {
                MessageBox.Show("snmp密码长度至少为8");
                return false;
            }
            return true;
        }

        public virtual bool Update(bool bView = true)
        {
            bool bSuccess = true;
            if (bView)
            {
                textBoxMac.Text = m_apinfo.mark;
                string strIp = m_apinfo.manage_ip;
                if (strIp != null)
                {
                    byte[] ip = IpByteByStr(strIp);
                    if (ip != null)
                    {
                        ipAddressControlIp.SetAddressBytes(ip);
                    }
                }
                comboBoxFloodID.Text = m_apinfo.floorid.ToString();
                textBoxRoomID.Text = m_apinfo.roomid.ToString();
                textBoxSnmpUser.Text = m_apinfo.snmp_user;
                textBoxSnmpPasswd.Text = m_apinfo.snmp_password;
            }
            else
            {
                if (CheckInputValid())
                {
                    m_apinfo.mark = textBoxMac.Text;
                    byte[] ip = ipAddressControlIp.GetAddressBytes();
                    string strIp = IpStrByByte(ip);
                    if (strIp != null)
                    {
                        m_apinfo.manage_ip = strIp;
                    }
                    m_apinfo.floorid = comboBoxFloodID.Text;
                    m_apinfo.roomid = textBoxRoomID.Text;
                    m_apinfo.snmp_user = textBoxSnmpUser.Text;
                    m_apinfo.snmp_password = textBoxSnmpPasswd.Text;
                }
                else
                {
                    bSuccess = false;
                }
            }
            return bSuccess;
        }

        public DbWrapper.DbDataTypes.Ap GetInfo()
        {
            return m_apinfo;
        }

        public byte[] IpByteByStr(string strIp)
        {
            try
            {
                System.Net.IPAddress a = System.Net.IPAddress.Parse(strIp);
                return a.GetAddressBytes();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                return null;
            }
        }
        public string IpStrByByte(byte[] ip)
        {
            System.Net.IPAddress a = new System.Net.IPAddress(ip);
            return a.ToString();
        }

        private void buttonModifySnmp_Click(object sender, EventArgs e)
        {
            textBoxSnmpUser.Enabled = true;
            textBoxSnmpPasswd.Enabled = true;
        }
    }
}
